#! /usr/bin/python
"""
This is the very top of the program.... the UI
"""

import utils
from level1 import *

try:
	import pygtk
	pygtk.require('2.0')
except:
	utils.err("Modul pygtk 2.0 not found! Please install pygtk2.0!")
	
try:
	import gtk
	import gtk.glade
except:
	utils.err("Modul gtk not found! Please install gtk!")

import os

class MainWindow:
	def destroy(self, widget, data=None):
		utils.exit()
		gtk.main_quit()

	def __init__(self):
		# Window settings
		self.xml = gtk.glade.XML('ui.glade')
		
		self.window = self.xml.get_widget("window")
		self.window.connect("destroy", self.destroy)
		
		# Menubar
		#uiManager = gtk.UIManager()
		#self.xml.get_widget("quit").connect("clicked", self.destroy)
		
		#self.window.add_accel_group(uiManager.get_accel_group())
		#actiongroup = gtk.ActionGroup("MB")
		#actiongroup.add_actions([("File", None, "_File"),
		#						("Exit", gtk.STOCK_QUIT, "_Exit", None, "", self.destroy),
		#						("Logs", None, "_Logs"),
		#						("Delete logs", None, "_Delete logs", None, "", self.DeleteLogs),
		#						("Show logs", None, "_Show logs", None, "", self.ShowLogs),
		#						("?", None, "_?"),
		#						("Credits", None, "_Credits", None, "", self.Credits),
		#						("About", None, "_About", None, "", self.About)
		#						])
		#uiManager.insert_action_group(actiongroup, 0)
		#uiManager.add_ui_from_string(self.ui)
		#menubar = uiManager.get_widget("/MenuBar")
		#vbox.pack_start(menubar, False, False, 0)
		
		# the "shell"
		self._txt = self.xml.get_widget("TextView")
		
		# Sliders
		self.Sliders = dict()
		self.Sliders["ClawF"] = self.xml.get_widget("hsClawF")
		self.Sliders["Claw"] = self.xml.get_widget("hsClaw")
		self.Sliders["Wrist"] = self.xml.get_widget("hsWrist")
		self.Sliders["WristR"] = self.xml.get_widget("hsWristR")
		self.Sliders["Elbow"] = self.xml.get_widget("hsElbow")
		self.Sliders["Shoulder"] = self.xml.get_widget("hsShoulder")
		self.Sliders["Base"] = self.xml.get_widget("hsBase")
		
		# Robot
		self.TopView = self.xml.get_widget("TopView") # top-view of the robot
		self.SideView = self.xml.get_widget("SideView") # side-view of the robot
		
		# Bind events to functions
		# Register Direct
		self.xml.get_widget("cmdParkD").connect("clicked", self.Park, None)
		self.xml.get_widget("cmdStopD").connect("clicked", self.Stop, None)
		self.xml.get_widget("cmdClawClose").connect("clicked", self.ClawClose, None)
		self.xml.get_widget("cmdClawOpen").connect("clicked", self.ClawOpen, None)
		self.xml.get_widget("cmdWristUp").connect("clicked", self.WristUp, None)
		self.xml.get_widget("cmdWristDown").connect("clicked", self.WristDown, None)
		self.xml.get_widget("cmdWristCCW").connect("clicked", self.WristCCW, None)
		self.xml.get_widget("cmdWristCW").connect("clicked", self.WristCW, None)
		self.xml.get_widget("cmdElbowUp").connect("clicked", self.ElbowUp, None)
		self.xml.get_widget("cmdElbowDown").connect("clicked", self.ElbowDown, None)
		self.xml.get_widget("cmdShoulderUp").connect("clicked", self.ShoulderUp, None)
		self.xml.get_widget("cmdShoulderDown").connect("clicked", self.ShoulderDown, None)
		self.xml.get_widget("cmdBaseLeft").connect("clicked", self.BaseLeft, None)
		self.xml.get_widget("cmdBaseRight").connect("clicked", self.BaseRight, None)
		
		# Register Sliders
		self.xml.get_widget("cmdGo").connect("clicked", self.Go, None)
		self.xml.get_widget("cmdClear").connect("clicked", self.Clear, None)
		self.xml.get_widget("cmdPark").connect("clicked", self.Park, None)
		self.xml.get_widget("cmdStop").connect("clicked", self.Stop, None)
	
	# Delete all logs in app-path/logs
	def DeleteLogs(self,widget,data=None):
		if self.MsgBox(prompt="This deletes alle log-files,\n"+
		"except the current one!") != gtk.RESPONSE_OK:
			return
		else:
			utils.deletelogs()
			return
	
	# Shows a list of all logs TODO
	def ShowLogs(self,widget,data=None):
		return
	
	# Shows the credits TODO
	def Credits(self,widget,data=None):
		return
	
	# Shows version and other stuff TODO
	def About(self,widget,data=None):
		return
	
	# Execute the commands! TODO
	def Go(self,widget,data=None):
		return
	
	# Clears als entrys
	def Clear(self,widget,data=None):
		for i in self.Sliders.values():
			i.set_value(0)
		return
	
	# Park the Robot TODO
	def Park(self,widget,data=None):
		return
	
	# Stops the robot immediately! TODO
	def Stop(self,widget,data=None):
		return
	
	def ClawClose(self,widget,data=None):
		utils.log(str(widget.name))
		
	def ClawOpen(self,widget,data=None):
		utils.log(str(widget.name))
		
	def WristUp(self,widget,data=None):
		utils.log(str(widget.name))
	
	def WristDown(self,widget,data=None):
		utils.log(str(widget.name))
	
	def WristCCW(self,widget,data=None):
		utils.log(str(widget.name))
	
	def WristCW(self,widget,data=None):
		utils.log(str(widget.name))
	
	def ElbowUp(self,widget,data=None):
		utils.log(str(widget.name))
	
	def ElbowDown(self,widget,data=None):
		utils.log(str(widget.name))
	
	def ShoulderUp(self,widget,data=None):
		utils.log(str(widget.name))
	
	def ShoulderDown(self,widget,data=None):
		utils.log(str(widget.name))
	
	def BaseLeft(self,widget,data=None):
		utils.log(str(widget.name))
	
	def BaseRight(self,widget,data=None):
		utils.log(str(widget.name))
	
	# Shows a simple messagebox
	def MsgBox(self, prompt="", buttons=gtk.BUTTONS_OK_CANCEL, icon=gtk.MESSAGE_WARNING):
		dialog = gtk.MessageDialog(self.window,
									gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
									icon,
									buttons,
									prompt)
		dialog.show()
		answer = dialog.run()
		dialog.destroy()
		return answer
	
	# Shows a messagebox with an error
	def ErrorBox(self, prompt=""):
		return self.MsgBox(prompt, gtk.BUTTONS_OK, gtk.MESSAGE_ERROR)
	
	def write(self,msg=""):
		output = self._txt.get_buffer()
		output.insert(output.get_end_iter(),msg)
		try:
			mark = output.get_mark("end")
			output.move_mark(mark, output.get_end_iter())
		except:
			mark = output.create_mark("end", output.get_end_iter())
		self._txt.scroll_to_mark(mark,0)
		
	def main(self):
		gtk.main()

if __name__ == "__main__":
	Me = MainWindow()
	
	utils.log_Callback = Me.write
	utils.error_Callback = Me.ErrorBox
	
	utils.log("UI started")
	Me.main()
